package com.epfresh.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.ListGoods;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/2/20
 * Desc:
 */
public class SearchGoodsViewBinder extends ItemViewBinder<ListGoods, SearchGoodsViewBinder.ViewHolder> {
    private OnCartClickListener onCartClickListener;
    private Context context;

    public SearchGoodsViewBinder(Context context){
        this.context=context;
    }

    public void setOnCartClickListener(OnCartClickListener onCartClickListener) {
        this.onCartClickListener = onCartClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_search_goods, parent, false);
        return new ViewHolder(itemView);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final ListGoods item) {
        ImageLoader.getInstance().displayImage(item.getThumbnail(), holder.iv_product);
        holder.tv_product_title.setText(item.getProductParseDto().getTitle());
        holder.tv_home_often_store.setText(item.getStoreName());
        // 1促销  2秒杀
        if(!TextUtils.isEmpty(item.getPromotionItemType())&&(item.getPromotionItemType().equals("1") ||item.getPromotionItemType().equals("2"))){
            holder.iv_product_url.setVisibility(View.VISIBLE);
            holder.tv_promotion.setVisibility(View.VISIBLE);
            holder.tv_price_old.setVisibility(View.VISIBLE);
            if(item.getPromotionItemType().equals("2")){
                holder.tv_promotion.setText("秒");
            }
            ImageLoader.getInstance().displayImage(item.getPromotion().getTagImageUrl(), holder.iv_product_url);
            holder.tv_price_old.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
            holder.tv_price_old.getPaint().setAntiAlias(true);
            holder.tv_price.setText("¥"+item.getPriceOnly() + "元/" + item.getChargeUnit());
            holder.tv_price_old.setText("¥"+item.getOriginalPrice()+ "元/" + item.getChargeUnit());
        }else{
            holder.iv_product_url.setVisibility(View.GONE);
            holder.tv_promotion.setVisibility(View.GONE);
            holder.tv_price_old.setVisibility(View.GONE);
            if (item.getProductSpecPrice().isPrePrice()) {
                holder.tv_price.setText("¥"+item.getProductSpecPrice().getPrice() + "元/" + item.getChargeUnit());
            } else {
                holder.tv_price.setText("时价");
            }
        }


        if (item.getName() == null || item.getName().equals("默认")) {
            holder.tv_unit_package.setText(item.getPack());
        } else {
            holder.tv_unit_package.setText(item.getName() + "(" + item.getPack() + ")");
        }


        String goodsCount = item.getGoodsCount();
        if ("".equals(goodsCount)) {
            updateCartStatus(holder, false);
        } else {
            updateCartStatus(holder, true);
            holder.ed_price.setText(goodsCount);
            holder.tv_charge_number.setText("已选" + goodsCount + item.getChargeUnit());
        }
        //判断是不是暂不接单  1是接单
       final int isAcceptOrders=item.getIsAcceptOrders();
        if(isAcceptOrders==1){
            holder.iv_add.setImageResource(R.mipmap.price_add);
        }else{
            holder.iv_add.setImageResource(R.mipmap.price_grey);
        }
//        WAITING(1, "待促销"), UNDERWAY(2, "促销中"), OVER(3, "促销结束"), SELL_OUT(4, "已抢光");
        if ("3".equals(item.getPromotionItemStatus())) {
            holder.tv_promotion_status.setVisibility(View.VISIBLE);
            holder.tv_promotion_status.setText("结束了");
            updateCartStatus(holder,false);
        } else if ("4".equals(item.getPromotionItemStatus())) {
            holder.tv_promotion_status.setVisibility(View.VISIBLE);
            holder.tv_promotion_status.setText("抢光了");
            updateCartStatus(holder,false);
        } else {
            holder.tv_promotion_status.setVisibility(View.GONE);
        }

        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isAcceptOrders==1){
                    updateCartStatus(holder, true);
                    if (onCartClickListener != null) {
                        onCartClickListener.OnCartClick(0, item, v);
                    }
                }else{
                    Toast.makeText(context,"暂不接单",Toast.LENGTH_SHORT).show();
                }

            }
        });

        holder.iv_add_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartClickListener != null) {
                    onCartClickListener.OnCartClick(0, item, v);
                }
            }
        });
        holder.iv_remove_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.ed_price.getText().equals("")) {
                    updateCartStatus(holder, false);
                }
                if (onCartClickListener != null) {
                    onCartClickListener.OnCartClick(1, item, v);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartClickListener != null) {
                    onCartClickListener.OnCartClick(2, item, v);
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_product,iv_product_url;
        private TextView tv_product_title, tv_price,tv_price_old, tv_unit_package, tv_home_often_store,tv_promotion,tv_promotion_status;
        private ImageView iv_add_count, iv_remove_count, iv_add;
        private TextView ed_price, tv_charge_number;
        private View item_center_search_underline_layout;

        ViewHolder(View itemView) {
            super(itemView);

            iv_product = itemView.findViewById(R.id.iv_product);
            iv_product_url=itemView.findViewById(R.id.iv_product_url);
            tv_product_title = itemView.findViewById(R.id.tv_product_title);
            tv_promotion = itemView.findViewById(R.id.tv_promotion);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_price_old = itemView.findViewById(R.id.tv_price_old);
            tv_unit_package = itemView.findViewById(R.id.tv_unit_package);
            tv_home_often_store = itemView.findViewById(R.id.tv_home_often_store);

            iv_add_count = itemView.findViewById(R.id.iv_add_count);
            tv_promotion_status=itemView.findViewById(R.id.tv_promotion_status);
            iv_remove_count = itemView.findViewById(R.id.iv_remove_count);
            iv_add = itemView.findViewById(R.id.iv_add);
            ed_price = itemView.findViewById(R.id.ed_price);
            tv_charge_number = itemView.findViewById(R.id.tv_charge_number);
            item_center_search_underline_layout = itemView.findViewById(R.id.item_center_search_underline_layout);
        }
    }

    private void updateCartStatus(ViewHolder holder, boolean isShow) {
        if (isShow) {
            holder.iv_add_count.setVisibility(View.VISIBLE);
            holder.iv_remove_count.setVisibility(View.VISIBLE);
            holder.iv_add.setVisibility(View.GONE);
            holder.ed_price.setVisibility(View.VISIBLE);
            holder.tv_charge_number.setVisibility(View.VISIBLE);
            holder.item_center_search_underline_layout.setVisibility(View.VISIBLE);
        } else {
            holder.iv_add_count.setVisibility(View.GONE);
            holder.iv_remove_count.setVisibility(View.GONE);
            holder.iv_add.setVisibility(View.VISIBLE);
            holder.ed_price.setVisibility(View.GONE);
            holder.tv_charge_number.setVisibility(View.GONE);
            holder.item_center_search_underline_layout.setVisibility(View.GONE);
        }
    }

    public interface OnCartClickListener {
        void OnCartClick(int type, ListGoods item, View view);
    }


}
